﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Flexibles.CApFlexCableNameGenerator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;


namespace DnProcessSimulateCommands.Flexibles;

internal class CApFlexCableNameGenerator
{
  private string m_cablePrefix;

  internal CApFlexCableNameGenerator() => this.m_cablePrefix = CApFLEXStringTable.CableName;

  public string GenerateNewCableName()
  {
    List<int> indexFromLayerNames = this.GetIndexFromLayerNames(TxFlxCableEx.CableNames());
    int num = 1;
    for (int index = 0; index < indexFromLayerNames.Count && indexFromLayerNames.IndexOf(num) != -1; ++index)
      ++num;
    string cablePrefix = this.m_cablePrefix;
    if (num != 1 || indexFromLayerNames.Count != 0)
      cablePrefix += num.ToString();
    return cablePrefix;
  }

  private List<int> GetIndexFromLayerNames(List<string> names)
  {
    List<int> indexFromLayerNames = new List<int>();
    for (int index = 0; index < names.Count; ++index)
      indexFromLayerNames.Add(this.GetIndexFromLayerName(names[index]));
    return indexFromLayerNames;
  }

  private int GetIndexFromLayerName(string layerName)
  {
    int indexFromLayerName = -1;
    if (layerName.StartsWith(this.m_cablePrefix))
    {
      int length1 = this.m_cablePrefix.Length;
      int length2 = layerName.Length - length1;
      string s = layerName.Substring(length1, length2);
      try
      {
        indexFromLayerName = int.Parse(s);
      }
      catch (Exception ex)
      {
        indexFromLayerName = -1;
      }
    }
    return indexFromLayerName;
  }
}
